package com.example.bailin.giftsay.sort.activity.SearchFragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.entity.SelectGiftEntity;
import com.example.bailin.giftsay.main.UpDataEvent;
import com.example.bailin.giftsay.main.adapter.ShowAllAdapter;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;
import com.example.bailin.giftsay.tool.UrlProvide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by bailin on 16/8/26.
 */
public class DPFragment extends BaseFragment {
    private RecyclerView rvSearchDP;
    private ShowAllAdapter adapter;

    @Override
    protected int setLayout() {
        return R.layout.fragment_dp;
    }

    @Override
    protected void initView() {
        rvSearchDP = bindView(R.id.rv_search_dp);
    }

    public DPFragment() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getType(UpDataEvent upDataEvent) {
        if (upDataEvent.getType() == 0) {
            adapter.setIndex(upDataEvent.getPosition());
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(String upDataEvent) {

        tool().getData(UrlProvide.SEARCH_CONTENT2 + upDataEvent, SelectGiftEntity.class, new NetTool.NetInterface<SelectGiftEntity>() {
            @Override
            public void onSuccess(SelectGiftEntity selectGiftEntity) {

                adapter = new ShowAllAdapter();
                adapter.setEntity(selectGiftEntity);
                GridLayoutManager manager = new GridLayoutManager(MyApp.getContext(), 2);
                rvSearchDP.setLayoutManager(manager);
                rvSearchDP.setAdapter(adapter);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
